<template>
  <div class="box-all">
    <el-dialog width="600px" title="新增代充记录" v-model="dialogFormVisible" :close-on-click-modal="false" :draggable="true"
      @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="代充商户" prop="toShopId">
                <el-select style="width:90%;" filterable v-model="dialogForm.toShopId" placeholder="请选择代充商户" @change="shopChange">
                  <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="账户余额">
                <el-input :disabled="true" style="width:90%;" type="number" v-model="balance" placeholder="账户余额">
                  <template #append>元</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="支付金额" prop="fee">
                <el-input style="width:90%;" type="number" v-model="dialogForm.fee" placeholder="请输入代充金额">
                  <template #append>元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付时间" prop="chargeTime">
                <el-date-picker style="width:90% !important;" v-model="dialogForm.chargeTime" type="datetime"
                  placeholder="请选择交易时间" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="支付方式" prop="payType">
                <el-input style="width:90%;" v-model="dialogForm.payType" placeholder="请输入支付方式" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付流水号" prop="tradeNo">
                <el-input style="width:90%;" v-model="dialogForm.tradeNo" placeholder="请输入交易流水" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="dialogForm.remark" placeholder="请输入备注" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
      <confirm-pass ref="confirmPassRef" @verification="dc" />
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import { successDeal, warningDeal } from '@/utils/utils'
import confirmPass from '@/components/confirmPass.vue'
import api from '../api'
export default {
  components: {
    confirmPass,
  },
  setup(props, context) {
    const dialogFormRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      balance: null,
      dialogFormVisible: false,
      dialogForm: {},
      shopOptions: [],
      dialogFormRules: {
        toShopId: [
          { required: true, message: '请选择代充店铺', trigger: 'blur' },
        ],
        fee: [
          { required: true, message: '请输入代充金额', trigger: 'blur' },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?$/, message: '请输入最多两位小数的正数', trigger: 'blur'
          }
        ],
        payType: [
          { required: true, message: '请输入支付方式', trigger: 'blur' },
        ],
        chargeTime: [
          { required: true, message: '请选择交易时间', trigger: 'blur' },
        ],
        tradeNo: [
          { required: true, message: '请输入交易流水', trigger: 'blur' },
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' },
        ],
      },
    })

    function dialogShow() {
      getShop()
      state.dialogFormVisible = true
    }

    function dialogClose() {
      dialogFormRef.value.resetFields()
    }

    function getShop() {
      api.chooseShop().then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function confirmPass(index, data) {
      let title = ''
      let msg = ''
      title = '店铺代充提示'
      msg = '是否确认该店铺代充？'
      confirmPassRef.value.dialogShow(title, msg)
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          confirmPass()
        }
      })
    }

    function dc() {
      const obj = {
        ...state.dialogForm,
        chargeTime: state.dialogForm.chargeTime.getTime(),
        fee: state.dialogForm.fee * 100,
      }
      api.accountChargeSubmit(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          context.emit('dialogClose', true)
          state.dialogFormVisible = false
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function shopChange(toShopId) {
      api.inquireBalance({ toShopId }).then(res => {
        if (res.code == '200') {
          state.balance = res.data.leftFee / 100
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      confirmPassRef,
      dialogShow,
      dialogClose,
      submitHandle,
      dc,
      shopChange,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-input) {
  .el-input-group__append {
    padding: 0 20px !important;
  }
}
</style>